﻿using System;
using System.ServiceModel;
using Xray.Common;

namespace Xray.Viewer
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class ContainerXrayService : IContainerXrayService
    {
        private readonly IModelUpdate _modelView;
        private readonly ModelManager _modelManager = new ModelManager(new Model());

        public ContainerXrayService(IModelUpdate model)
        {
            _modelView = model;
        }

        /// <summary>
        /// Update the component model
        /// </summary>
        /// <param name="model">the entire current model (not a delta)</param>
        public void Update(Model model)
        {
            _modelManager.ApplyUpdatedModel(model);
            ModelDeltas lastModelDelta = _modelManager.LastModelDelta();
            _modelView.UpdateModel(lastModelDelta);
        }

        /// <summary>
        /// Clears the current model. allowing to start from scratch
        /// </summary>
        public void ClearModel()
        {
            _modelManager.ApplyUpdatedModel(new Model());
            _modelView.Clear();
        }
    }
}